package com.example.wannerone.myapplication123;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.wannerone.http.HttpPostUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MembersActivity extends AppCompatActivity {

    private String result;
    ListView listView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_members);

        listView = (ListView) findViewById(R.id.listView);
        //全局变量
        MyApplication application = (MyApplication) getApplication();
        System.out.println("application.getU_id=" + application.getU_id());
        final Map<String, Object> userInfo = new HashMap<String, Object>();
        userInfo.put("u_id", application.getU_id());
        String jsonString = JSONObject.toJSONString(userInfo);
        final String info = "info=" + jsonString;
        /*这里需要留意的是httpPostUtils请求在Android里面不能放在主线程里面，必须新建一个子线程，然后通过Hanlder把子线程的值传过来更新UI（因为子线程不能直接更改UI）*/
        new Thread() {
            public void run() {
                String response =
                        HttpPostUtils.doPostRequest("/user/listMembers", info);
                Message message = Message.obtain();
                message.obj = response;
                mHanlder.sendMessage(message);
            }
        }.start();


    }


    private Handler mHanlder = new Handler() {
        @Override
        public void handleMessage(Message message) {
            result = (String) message.obj;
            JSONObject resultJson = JSON.parseObject(result);
            String finalResult = resultJson.getString("result");
            System.out.println("结果是：" + finalResult);
            if (finalResult.equals("ok")) {
                JSONArray jsonArray = resultJson.getJSONArray("userInfoList");
                List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
                for (Object obj : jsonArray) {
                    JSONObject jsonObject = (JSONObject) obj;
                    //System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
//                    String kPassword = jsonObject.getString("kPassword");
//                    String kAvailableTimes = jsonObject.getString("kAvailableTimes");
//                    String kFailureTime = jsonObject.getString("kFailureTime");
                    String name=jsonObject.getString("uName");
                    System.out.println("name==="+name);
                    HashMap<String, Object> item = new HashMap<String, Object>();
//                    item.put("kPassword", kPassword);
//                    if (!kAvailableTimes.equals("-2")) {
//                        item.put("kAvailableTimes", kAvailableTimes);
//                    } else {
//                        item.put("kAvailableTimes", "");
//                    }
//                    item.put("kFailureTime", kFailureTime);
                    item.put("name",name);
                    data.add(item);
                }
                //创建SimpleAdapter适配器将数据绑定到item显示控件上
                SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), data, R.layout.item_members,
                        new String[]{"name"}, new int[]{R.id.name});

                //实现列表的显示
                listView.setAdapter(adapter);
                //条目点击事件
                //listView.setOnItemClickListener(new ItemClickListener());
            } else
                System.out.println("用户名或密码错误");
        }
    };
}
